package com.metadata_system.system.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.common.db.entity.Metadata;
import com.metadata_system.common.db.entity.MetadataEdge;
import com.metadata_system.common.db.entity.MetadataNode;
import com.metadata_system.common.enums.AllConst;
import com.sun.javafx.geom.Edge;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class MetadataDetailResponse {
    private Integer id;
    private String name;
    private String shortName;
    private Object currView;
    private List<ViewItem> viewArr;
    private Integer viewTotal;

    private List<NodeItem> nodes;
    private List<EdgeItem> edges;
    private List<NodeItem> allNodes;
    private List<EdgeItem> allEdges;

    private List<Object> enableAllNodes;
    private Object enableAllEdges;
    private String defaultNodeType;
    private Integer defaultEdgeType;



    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    public static MetadataDetailResponse fromEntity(Metadata metadata, List<MetadataNode> allNodes, List<MetadataEdge> allEdges,Integer viewId) {
        MetadataDetailResponse response = new MetadataDetailResponse();
        response.setId(metadata.getId());
        response.setName(metadata.getName());
        response.setShortName(metadata.getShortName());
        response.setViewArr(JSON.parseArray(metadata.getViewArr(),ViewItem.class));
        response.setViewTotal(metadata.getViewTotal());
        response.setCreateTime(metadata.getCreatedTime());

        response.setAllEdges(EdgeItem.fromEntity(allEdges));
        response.setAllNodes(NodeItem.fromEntity(allNodes));


        List<EdgeItem> edges = new ArrayList<>();
        for (MetadataEdge edge : allEdges) {
            if (edge.getMetadataViewId().equals(viewId)) {
                edges.add(EdgeItem.fromEntity(edge));
            }
        }
        response.setEdges(edges);

        List<NodeItem> nodes = new ArrayList<>();
        for (MetadataNode metadataNode : allNodes) {
            if (metadataNode.getMetadataViewId().equals(viewId)) {
                nodes.add(NodeItem.fromEntity(metadataNode));
            }
        }
        response.setNodes(nodes);

        response.setEnableAllEdges(JSON.parseObject(metadata.getEnableEdgeArr()));
        response.setEnableAllNodes(JSON.parseArray(AllConst.EnableNodeArrString));
        response.setDefaultEdgeType(1);
        response.setDefaultNodeType("default");

        ViewItem currView = ViewItem.builder().build();
        for (ViewItem viewItem : response.getViewArr()) {
            if (viewItem.getId().equals(viewId)) {
                currView = viewItem;
            }
        }
        response.setCurrView(currView);
        return response;
    }


}
